﻿using System.Drawing;
using XFControls.Utils;
using XFControls.XFPanels.XFPanelItems;
using XFControls.XFPanels;

namespace XFSenseUI.XFItems.SenseControls
{
    public class SenseControlDropDownEffect : SenseItemBase
    {
        private int _height;
        private int _localY;
        private int _localTopPadding;
        private string _display;

        public SenseControlDropDownEffect(int height, int localY, int localTopPadding, string display)
        {
            _localTopPadding = localTopPadding;
            _localY = localY;
            _height = height;
            _display = display;
            Style = new XFItemStyle
                    {
                        TextFont = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular),
                        TextColor = Color.Black,
                        Padding = 10
                    };
        }

        public override void ItemPaint(Graphics g, int xOrg, int y)
        {
            var ddRec = this.GetImageRec("dropdown-selected");
            var menuRec = this.GetImageRec("dropdownmenu");
            var menuBottomRec = this.GetImageRec("dropdownmenu-bottom");

            int x = (GetWidth() >> 1) - (ddRec.Width >> 1);
            int menuHeight = _height - (Style.PaddingTop + menuBottomRec.Height + _localTopPadding);

            Rectangle rec = new Rectangle(x, _localY + Style.PaddingTop + _localTopPadding, menuRec.Width, menuHeight);
            DrawUI("dropdownmenu", g, rec, false);

            rec = new Rectangle(x, _localY + _height - menuBottomRec.Height, menuBottomRec.Width, menuBottomRec.Height);
            DrawUI("dropdownmenu-bottom", g, rec, false);

            rec = new Rectangle(x, _localY + _localTopPadding, ddRec.Width, ddRec.Height);
            DrawUI("dropdown-selected", g, rec, false);

            if (!string.IsNullOrEmpty(_display))
            {
                using (SolidBrush b = new SolidBrush(Style.TextColor))
                    g.DrawString(_display, Style.TextFont, b, new RectangleF(Style.PaddingLeft + x, Style.PaddingTop + _localY + _localTopPadding, ddRec.Width, ddRec.Height));
            }
        }

        public override void OnMouseUp()
        {
            if (Parent is XFPanelList)
                ((XFPanelList)Parent).OnBlur();
            else
                Parent.ClearEffect(this);
        }
    }
}
